package com.ficus.road.maintain.third.zh.roadice;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.AllFactoryTypeEnum;
import com.ficus.road.maintain.core.model.Hit;
import com.ficus.road.maintain.core.model.IceAndBridgeProcessZH;
import com.ficus.road.maintain.dao.HitAllFactoryDao;
import com.ficus.road.maintain.strategy.allfactory.AllFactoryContext;
import com.ficus.road.maintain.strategy.allfactory.IceAndBridgeStrategy;
import com.ficus.road.maintain.third.zh.BaseZH;
import com.ficus.road.maintain.util.MyDateUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/3/31 下午8:51
 */
@Component
public class ZHRoadIceProcess extends BaseZH {

    @Autowired
    private HitAllFactoryDao hitAllFactoryDao;

    @PostConstruct
    void init() {
        arg0 = "sanwangComplainData";
        errMsg = "调用中海接口获取路面结冰处置流程异常!";
        successMsg = "调用中海接口获取路面结冰处置流程成功!";
    }

    //    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "0 0/30 * * * ?")
    public void refreshBridgeHotelProcess() {
        String dateStr = MyDateUtil.dateFormatStr(new Date(), "yyyy-MM-dd");
        arg1 = "{\"complainttypeflag\":\"6\",\"startTime\":\"" + dateStr + "\",\"endTime\":\"" + dateStr + "\"}";
        String roadIceProcessStr = getZHData();
        if (roadIceProcessStr == null) {
            return;
        }
        List<IceAndBridgeProcessZH> list = JSONArray.parseArray(roadIceProcessStr, IceAndBridgeProcessZH.class);
        if (ObjectUtils.isEmpty(list)) {
            return;
        }
        AllFactoryContext allFactoryContext = new AllFactoryContext(new IceAndBridgeStrategy(hitAllFactoryDao, AllFactoryTypeEnum.ROAD_ICE.getType()));
//        AllFactoryContext allFactoryContext = new AllFactoryContext(hitAllFactoryDao, AllFactoryTypeEnum.ROAD_ICE);
        List<Hit> hitList = list.stream().map(allFactoryContext::convert2AllFactoryHit).collect(Collectors.toList());
        hitList.parallelStream().forEach(hit -> {
            hitAllFactoryDao.saveOrUpdate(hit);
        });
    }
}
